package com.heitian.ssm.controller;

import com.heitian.ssm.dao.UserDao;
import com.heitian.ssm.model.Call;
import com.heitian.ssm.model.JsonResponse;
import com.heitian.ssm.model.User;
import com.heitian.ssm.service.UserService;
import jdk.nashorn.internal.codegen.CompilerConstants;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Zhangxq on 2016/7/15.
 */

@Controller
@RequestMapping("/user")
public class UserController {

    private Logger log = Logger.getLogger(UserController.class);
    @Resource
    private UserService userService;
    @Resource
    private UserDao userDao;

    @RequestMapping(method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResponse selectUser(HttpServletRequest request, Model model) {
        log.info("查询所有用户信息");
        List<User> userList = userService.getAllUser();
        return new JsonResponse(JsonResponse.SUCCESS, userList);
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResponse addUser(@RequestParam String name, @RequestParam String password) {
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        int isok = userDao.insertUser(user);
        int uid = user.getId();
        return new JsonResponse(JsonResponse.SUCCESS, uid);
    }

    @RequestMapping("/login")
    @ResponseBody
    public JsonResponse login(@RequestParam String name,
                              @RequestParam String password) {

        User user = userDao.findByNameAndPassword(name, password);

        if (null == user) {
            return new JsonResponse(JsonResponse.WRONG_LOGIN_INFO_USER, null);
        } else {
            userDao.login(user);
            user = userDao.findByNameAndPassword(name, password);
            return new JsonResponse(JsonResponse.SUCCESS, user);
        }
    }

    @RequestMapping("/logout/{id}")
    @ResponseBody
    public JsonResponse logout(@PathVariable("id") String id){

        int isok = userDao.logout(Integer.valueOf(id));
        return new JsonResponse(JsonResponse.SUCCESS, isok);
    }

    @RequestMapping(value = "/call", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResponse getJson(@RequestBody Call call) {
        Call res = call;

        int isok = userDao.save(res);
        return new JsonResponse(JsonResponse.SUCCESS, null);
    }

//    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
//    @ResponseBody
//    public JsonResponse saveJson(@RequestParam String str) {
//
//        int isok = userDao.saveMsg(str);
//        return new JsonResponse(JsonResponse.SUCCESS, null);
//    }


}
